﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using BaseData.ApplicationLib.Commands;
using BaseData.ApplicationLib.Queries;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;

namespace BaseData.WebApi.Controllers
{
    [ApiController]
    public class MaterialCategoryController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IMaterialCategoryQueries _materialCategoryQueries;

        public MaterialCategoryController(IMediator mediator,
            IMaterialCategoryQueries materialCategoryQueries)
        {
            _mediator = mediator;
            _materialCategoryQueries = materialCategoryQueries;
        }

        [HttpPost(ApiRoutes.MaterialCategory.CreateMaterialCategory)]
        public async Task<IActionResult> Post(CreateMaterialCategoryCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    
        [HttpGet(ApiRoutes.MaterialCategory.QueryMaterialCategories)]
        public async Task<IActionResult> Get([FromQuery] MaterialCategorySearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _materialCategoryQueries.QueryMaterialCategoriesByPage(model);
                return Ok(data);
            }
            else
            {
                var data = await _materialCategoryQueries.QueryMaterialCategories(model);
                return Ok(data);
            }
        }

        [HttpGet(ApiRoutes.MaterialCategory.GetMaterialCategory)]
        public async Task<IActionResult> Get(int id)
        {
            var data = await _materialCategoryQueries.GetMaterialCategory(id);
            return Ok(data);
        }

        [HttpPut(ApiRoutes.MaterialCategory.UpdateMaterialCategory)]
        public async Task<IActionResult> Put(int id, [FromBody] UpdateMaterialCategoryCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    
        [HttpGet(ApiRoutes.MaterialCategory.TreeMaterialCategory)]
        public async Task<IActionResult> QueryMaterialCategoriesByTree()
        {
            var data = await _materialCategoryQueries.QueryMaterialCategoriesByTree();
            return Ok(data);
        }
    
    }
}